#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define D long double
#define int long long
#define N 2000006
int head[N],nxt[N],ver[N],tot,edg[N];
void add(int x,int y,int z){
	ver[++tot]=y;edg[tot]=z;
	nxt[tot]=head[x];head[x]=tot;
}
int dep[N],fth[N][21],qz[N];
void dfs(int x,int fa){
	dep[x]=dep[fa]+1;fth[x][0]=fa;
	for(int i=1;i<=20;i++)	fth[x][i]=fth[fth[x][i-1]][i-1];
	for(int i=head[x];i;i=nxt[i]){
		int y=ver[i],z=edg[i];
		if(y==fa)	continue;
		qz[y]=qz[x]+z;
		dfs(y,x);
	}
}
inline int LCA(int x,int y){
	if(x==y)	return x;
	if(dep[x]<dep[y])	swap(x,y);
	for(int i=20;i>=0;i--){
		if(dep[fth[x][i]]>=dep[y])	x=fth[x][i];
	}
	if(x==y)	return x;
	for(int i=20;i>=0;i--){
		if(fth[x][i]!=fth[y][i])	x=fth[x][i],y=fth[y][i];
	}
	return fth[x][0];
}
int st[N][21],pos[N],p2[31];
inline int dmin(int x,int y){
	return (dep[x]<dep[y])?x:y;
}
inline int dmax(int x,int y){
	return (dep[x]>dep[y])?x:y;
}
inline int getlr(int l,int r){
	int now=pos[r-l+1],x=l,y=r-p2[now]+1;
	//cerr<<"ASK: "<<l<<" "<<r<<" : "<<now<<" "<<p2[now]<<" : "<<l<<" "<<r-p2[now]+1<<endl;
	return dmin(st[x][now],st[y][now]);
}
inline int getd(int x,int y){
	int l=LCA(x,y);
	//cerr<<"NOW: "<<x<<" "<<y<<"  "<<l<<" : "<<qz[x]<<"+"<<qz[y]<<"-"<<2*qz[l]<<endl;
	return qz[x]+qz[y]-2*qz[l];
}
signed main(){
	freopen("memory.in","r",stdin);
	freopen("memory.out","w",stdout);
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n,q;cin>>n>>q;
	for(int i=1;i<n;i++){
		int x,y,z;cin>>x>>y>>z;add(x,y,z);add(y,x,z);
	}
	dfs(1,0);int now=0;p2[0]=1;
	for(int i=1;i<=n;i++){
		if((1<<(now+1))<=i){
			now++;p2[now]=p2[now-1]*2;
		}
		pos[i]=now;
	}
	for(int i=1;i<n;i++)	st[i][0]=LCA(i,i+1);
	for(int j=1;(1<<j)-1<=n;j++){
		for(int i=1;i+p2[j]-1<=n;i++){
			st[i][j]=dmin(st[i][j-1],st[i+p2[j-1]][j-1]);
		}
	}
	int lstans=0;
	while(q--){
		int p,l,r;cin>>p>>l>>r;p^=lstans;l^=lstans;r^=lstans;
		if(l==r){
			cout<<(lstans=getd(l,p))<<"\n";continue;
		}
		int lca=getlr(l,r-1),lp=LCA(lca,p);
		if(lp==lca){
			if(p==lca){
				cout<<(lstans=0)<<"\n";continue;
			}
			int mx=lca;
			for(int i=l;i<=r;i++)	mx=dmax(mx,LCA(i,p));
			//cerr<<"NOW: "<<mx<<" : "<<p<<"   "<<getd(mx,p)<<endl;
			cout<<(lstans=getd(mx,p))<<"\n";
		}else{
			cout<<(lstans=getd(lca,p))<<"\n";
		}
		cerr<<endl;
	}
	return 0;
}
